var mongoose = require("mongoose");
var Usercommentr = require('../modules/usercomment.module');


exports.create = function(req, res, next) {
    console.log(req.body)
    const user = new Usercommentr(req.body);
    user.save(function(err, data) {
        if (err) {
            console.log(err)
        } else {
            res.json(data);
        }
    })
}

exports.update = function(req, res, next) {
    const user = new Usercommentr(req.body);
    const id = req.params.id;

    Usercommentr.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
        res.json(data);
    });
};
exports.remove = function(req, res, next) {
    console.log(req.params.id)
    const id = req.params.id;
    
    Usercommentr.findByIdAndRemove(id, function(err, data) {
        res.json({ 'message': 'delete ok' });
    });
};
exports.list = function(req, res, next) {
     var page = (req.body.page) ? req.body.page : 1;
     var limit = (req.body.limit) ? req.body.limit : 3;
    var queryCondition = {};   
    console.log(req.body.useid)
    if (req.body.useid) {
        _id = req.body.useid;
        queryCondition = {
            'useid': _id
        }
    }
    Usercommentr.paginate(queryCondition, { page: page, limit: limit }, function(err, results) {
        res.json(results);  
       
    })

}
exports.login=function(req,res,next){
    console.log(req.body)
   var name=req.body.name;
   var password=req.body.password;
   Usercommentr.findOne({name:name,password:password},function(err,doc){console.log(doc)

       if(doc){
           res.json(doc)
       }else{
           res.json({'massage':'用户名或者密码错误！'})
       }

   })
   
}